package com.sxy.cookie; /**
 * @author 宋鑫宇
 * @version 1.0
 */

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.net.URLEncoder;

@WebServlet("/aServlet")
public class AServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Cookie的使用
        //1.创建Cookie对象，cookie不能直接存储中文,会报错,需进行编码设置
        String value = "张三";
        value = URLEncoder.encode(value,"utf-8");
        System.out.println("存储数据："+value);
        Cookie cookie = new Cookie("username", value);
        //设置cookie存活时间,存活一周的时间，到时间自动销毁
//        cookie.setMaxAge(60*60*24*7);
        //2.发送cookie到浏览器，
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
